<?php
/********************************************************
** Copyright 2010-2013. Todos los derechos reservados. **
** Desarrollado y Gestionado por:					   **
** Sugey Quintero (Analista)						   **
** José G. De Freitas (Programador)					   **
** Klaudia Uribe (Diseñadora Web)					   **
** Cesar Isturiz (Diseñador Web)					   **
**				S E A  v1.0							   **
*********************************************************/

require('fpdf/fpdf.php');
require('conexion.php');

	session_start();
	date_default_timezone_set('America/Caracas'); setlocale(LC_TIME, 'spanish');
	$usuario = $_SESSION['name'];
	$fecha = date("Y-m-d H:i:s");
	$auditoria = mysql_query("INSERT INTO auditoria SELECT '$usuario', '$fecha' , 'Pdf Listado de estudiantes' ");

class PDF extends FPDF{
var $widths;
var $aligns;

function SetWidths($w){
	//Set the array of column widths
	$this->widths=$w;
}

function SetAligns($a){
	//Set the array of column alignments
	$this->aligns=$a;
}

function Row($data){
	//Calculate the height of the row
	$nb=0;
	for($i=0;$i<count($data);$i++)
		$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
	$h=5*$nb;
	//Issue a page break first if needed
	$this->CheckPageBreak($h);
	//Draw the cells of the row
	for($i=0;$i<count($data);$i++){
		$w=$this->widths[$i];
		$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
		//Save the current position
		$x=$this->GetX();
		$y=$this->GetY();
		//Draw the border
		
		$this->Rect($x,$y,$w,$h);

		$this->MultiCell($w,5,$data[$i],0,$a,'true');
		//Put the position to the right of the cell
		$this->SetXY($x+$w,$y);
	}
	//Go to the next line
	$this->Ln($h);
}

function CheckPageBreak($h){
	//If the height h would cause an overflow, add a new page immediately
	if($this->GetY()+$h>$this->PageBreakTrigger)
		$this->AddPage($this->CurOrientation);
}

function NbLines($w,$txt){
	//Computes the number of lines a MultiCell of width w will take
	$cw=&$this->CurrentFont['cw'];
	if($w==0)
		$w=$this->w-$this->rMargin-$this->x;
	$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
	$s=str_replace("\r",'',$txt);
	$nb=strlen($s);
	if($nb>0 and $s[$nb-1]=="\n")
		$nb--;
	$sep=-1;
	$i=0;
	$j=0;
	$l=0;
	$nl=1;
	while($i<$nb)
	{
		$c=$s[$i];
		if($c=="\n")
		{
			$i++;
			$sep=-1;
			$j=$i;
			$l=0;
			$nl++;
			continue;
		}
		if($c==' ')
			$sep=$i;
		$l+=$cw[$c];
		if($l>$wmax)
		{
			if($sep==-1)
			{
				if($i==$j)
					$i++;
			}
			else
				$i=$sep+1;
			$sep=-1;
			$j=$i;
			$l=0;
			$nl++;
		}
		else
			$i++;
	}
	return $nl;
}

function Header(){
	//$this->Image('images/me.jpg' , 10 ,5 , 40 , 20 , 'JPG');
	$this->SetFont('Arial','B',15);
	$this->Cell(1);
	//$this->Cell(0,5,'E.B.N. "La Mora"',0,0,'C', 0);
	$this->Image('images/gob.jpg' , 13 ,3 , 250 , 15 , 'JPG'); //1ero pos en x, 2do pos en y, 3ero largo(width) de la img, 4to ancho (height) de la img
	$this->Ln(30);
}

function Footer(){
	//Pie de página
	$this->SetY(-15);
	$this->SetFont('Arial','I',10);
	$this->SetTextColor(128);
	$this->Cell(0,10,'Pagina '.$this->PageNo().' de {nb}',0,0,'C'); // el parametro {nb} es generado por una funcion llamada AliasNbPages
}

}
//variable mandada
$periodo=$_GET['periodo'];
$grado=$_GET['grado'];
$seccion=$_GET['seccion'];

	$pdf=new PDF('L','mm','Letter');
	$pdf->AliasNbPages();
	$pdf->Open();
	$pdf->AddPage();
	$pdf->SetMargins(20,20,20);
	$pdf->Ln(0);
	//$pdf->Cell(140);
	$pdf->Cell(0,5,'LISTADO DE ESTUDIANTES',0,0,'C', 0);
	$pdf->Ln(10);
	$pdf->SetFont('Arial','',10);
	$pdf->Cell(0,5,'PERIODO ESCOLAR: '.$periodo,0,0,'L', 0);
	$pdf->Ln(4);
	$pdf->Cell(0,5,utf8_decode(strtoupper($grado).' AÑO "'.$seccion.'"'),0,0,'L', 0);
	$pdf->Ln(10);
	$pdf->Cell(15);
	$pdf->SetWidths(array(10, 30, 50, 50, 80));
	$pdf->SetFont('Arial','B',10);
	$pdf->SetFillColor(178,178,178); //85,107,47
    $pdf->SetTextColor(0);

		for($i=0;$i<1;$i++){
				$pdf->Row(array('NRO', 'CEDULA', 'NOMBRES', 'APELLIDOS', 'DIRECCION'));
			}


	$Consulta = mysql_query("select CI_a, nombre_a, apellido_a, direccion_a from alumno INNER JOIN inscripcion ON CI_a=cedula_a where grado='$grado' and seccion='$seccion' and periodo='$periodo' order by CI_a");
	$numfilas = mysql_num_rows($Consulta);
	$cant=1;
	for ($i=0; $i<$numfilas; $i++){
			$fila = mysql_fetch_array($Consulta);
			$pdf->SetFont('Arial','',10);
			
				$pdf->SetFillColor(255,255,255);
    			$pdf->SetTextColor(0);
    			$pdf->Cell(15);
				$pdf->Row(array($cant, $fila['CI_a'], $fila['nombre_a'], $fila['apellido_a'], $fila['direccion_a']));
		$cant++;
		}
$pdf->Output();
?>
